package com.xd.cloud.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xd.cloud.common.bean.MessageBean;
import com.xd.cloud.common.util.DateUtil;
import com.xd.cloud.entity.device.Sim;
import com.xd.cloud.entity.device.SimPo;
import com.xd.cloud.service.device.SimService;

/**   
 * 功能描述：sim卡信息管理controller
 * @author: wangshanshan   
 * @date: 2017年12月14日 上午10:26:45
 */
@Controller
@RequestMapping("/sim")
public class SimController extends BaseController{

	@Autowired
	private SimService simService;
	
	/**
	 * 根据条件搜索信息列表
	 * @return Object
	 */
	@RequestMapping(value = "/searchList")
	@ResponseBody
	public Object searchList(HttpServletRequest request, HttpServletResponse response, String page, String limit) {
		try {
		    int pageNo = 0;
		    int pageSize = 0;
		    if(StringUtils.isNotEmpty(page)) {
		    	pageNo = Integer.parseInt(page);
		    }
		    if(StringUtils.isNotEmpty(limit)) {
		    	pageSize = Integer.parseInt(limit);
		    }
		    List<Sim> retuList = simService.getAllSim(pageNo, pageSize);
			if(retuList!=null && retuList.size()>0) {
				Map<Object, Object> info = new HashMap<Object, Object>();  
			    info.put("pageData", retuList);  
			    info.put("total", retuList.size()); 
				return info;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	/**
	 * 查看信息详情
	 * @return Object
	 */
	@RequestMapping(value = "/showDetail")
	@ResponseBody
	public Object showDetail(HttpServletRequest request, HttpServletResponse response,String id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			SimPo item = new SimPo();
			if(StringUtils.isNotEmpty(id) && !id.equals("-1")) {
				item = (SimPo)simService.getSimObjectById(Integer.valueOf(id));
				item.setRegistration_time_str(DateUtil.DateToString(DateUtil.format_YYYY_MM_DD_HH_MM_SS, item.getRegistration_time()));
			}
			msgBean.setSuccess(true);
			msgBean.setMsg(item);
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	/**
	 * 添加or修改数据
	 * @return Object
	 */
	@RequestMapping(value = "/saveOrUpdateInfo")
	@ResponseBody
	public Object saveOrUpdateInfo(HttpServletRequest request, HttpServletResponse response,String id,String sim_card,String company_id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			Sim item = new Sim();
			if(StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(sim_card)) {
				item.setSim_card(sim_card);
				item.setCompany_id(Integer.valueOf(company_id));
				item.setRegistration_time(DateUtil.stringToDate(request.getParameter("registration_time"),DateUtil.format_YYYY_MM_DD_HH_MM_SS));
				item.setStatus(Integer.valueOf(request.getParameter("status")==null?"0":request.getParameter("status")));
				item.setRemark(request.getParameter("remark"));
				item.setUpdate_time(new Date());
				if(id.equals("-1")) {//添加操作
					Sim temp = simService.getSimBySimCard(item.getSim_card());
					if(temp!=null) {//设备号存在，不允许添加
						msgBean.setMsg("SIM卡号已存在！");
						return msgBean;
					}
					item.setCreate_time(new Date());
					simService.save(item);
					msgBean.setSuccess(true);
				}else {//修改操作
					Sim temp = simService.getSimById(Integer.valueOf(id));
					item.setId(Integer.valueOf(id));
					item.setCreate_time(temp.getCreate_time());
					item.setInstall(temp.getInstall());
					simService.update(item);
					msgBean.setSuccess(true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	/**
	 * 删除操作
	 * @return Object
	 */
	@RequestMapping(value = "/deleteInfo")
	@ResponseBody
	public Object deleteInfo(HttpServletRequest request,String id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(id)) {
				boolean flag = simService.deleteInfo(Integer.valueOf(id));
				msgBean.setSuccess(flag);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return msgBean;
	}
	
	/**
	 * 未安装且已启用的(状态正常的)Sim卡列表
	 * @return Object
	 */
	@RequestMapping(value = "/getSimList")
	@ResponseBody
	public Object getSimList(HttpServletRequest request, HttpServletResponse response) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			List<SimPo> list = simService.getSimList();
			if(list!=null && list.size()>0) {
				msgBean.setSuccess(true);
				msgBean.setMsg(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	/**
	 * 快速保存sim卡信息
	 * 添加设备信息时，增加simCard信息
	 * @return Object
	 */
	@RequestMapping(value = "/fastSaveSim")
	@ResponseBody
	public Object fastSaveSim(HttpServletRequest request, HttpServletResponse response, String sim_card, String company_id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(sim_card) && StringUtils.isNotEmpty(company_id)) {
				Sim item = new Sim();
				item.setSim_card(sim_card);
				Sim temp = simService.getSimBySimCard(item.getSim_card());
				if(temp!=null) {//设备号存在，不允许添加
					msgBean.setMsg("SIM卡号已存在！");
					return msgBean;
				}
				item.setCompany_id(Integer.valueOf(company_id));
				item.setRegistration_time(new Date());
				item.setStatus(1);
				item.setCreate_time(new Date());
				simService.save(item);
				item = simService.getSim(sim_card, Integer.valueOf(company_id));
				if(item!=null && item.getSim_card().equals(sim_card)) {
					msgBean.setSuccess(true);
					msgBean.setMsg(item);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
}
